<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/21
 * Time: 9:20
 */

namespace App\Http\Controllers\Api\V1\Manager\Wx;


use App\Http\Controllers\Controller;
use App\Models\WxAutoResponse;
use Illuminate\Http\Request;

class AutoMenage extends Controller
{

    public function auto(){
        define("TOKEN", "weixin");
        $wechatObj = new WxApi();
        //$wechatObj->valid();
        $wechatObj->responseMsg();
    }




    public function index(Request $request)
    {

        $keyWords = $request->keyWords;
        $subscribe = $request->subscribe;
        $sql = WxAutoResponse::select();
        if ($keyWords){
            $sql->where('keyWords','like','%'.$keyWords.'%');
        }
        if ($subscribe){
            $sql->where('subscribe','1');
        }else{
            $sql->where('subscribe','<>','1');
        }
        $data = $sql->get()->toArray();
        return $this->apiResponse($data);
    }

    public function store(Request $request)
    {
        $data = $request->all();
        $responseId = $data['responseId'];
        unset($data['responseId']);
        if($responseId){//修改
            $status = WxAutoResponse::where('responseId',$responseId)->update($data);
        }else{
            $status = WxAutoResponse::create($data);
        }
        if($status){//新增成功
            return $this->apiResponse(true);
        }else{
            return $this->apiResponse('', config('errorCode.ADD_FAILED'));
        }

    }


    public function show($responseId)
    {
        $data = WxAutoResponse::find($responseId);

        return $this->apiResponse($data);
    }

    public function destroy($responseId)
    {

        $status = WxAutoResponse::destroy($responseId);
        if($status){//删除成功
            return $this->apiResponse(true);
        }else{
            return $this->apiResponse('', config('errorCode.NOR_FAILED'));
        }
    }

}